//
//  InSoundPlayingViewController.m
//  Inner  Voice
//
//  Created by 徐海宇 on 16/3/2.
//  Copyright © 2016年 徐海宇. All rights reserved.
//

#import "InSoundPlayingViewController.h"

#import "InSound.h"

#import "InSoundPlayingTableViewCell.h"

#define kIdentifier @"soundPlayingCell"

#import "AppDelegate.h"

#define appDelegate ((AppDelegate *)[[UIApplication sharedApplication]delegate])

@interface InSoundPlayingViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@property (nonatomic,strong)InSound *sound;

@end

@implementation InSoundPlayingViewController

- (NSMutableArray *)playingSoundArray {
    if (_playingSoundArray == nil) {
        _playingSoundArray = [NSMutableArray array];
    }
    return _playingSoundArray;

}
- (IBAction)backBtnClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)startBtnClick:(id)sender {
    InPlayerViewController *contro = [[InPlayerViewController alloc]init];
    [self presentViewController:contro animated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.myTableView registerNib:[UINib nibWithNibName:@"InSoundPlayingTableViewCell" bundle:nil] forCellReuseIdentifier:kIdentifier];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
   
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.playingSoundArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    InSoundPlayingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kIdentifier forIndexPath:indexPath];
   
    InSound *sound = self.playingSoundArray[indexPath.row];
    
    cell.InSoundName.text = sound.inSound_name;
    
    [cell loadImageWithStr:sound.inSound_icon];
//    [cell loadImageWithStr:@"http://echo-mx.b0.upaiyun.com/1768960618.jpg!100"];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [appDelegate.InPlayerViewController.inplayer stop];
    
    
#pragma warn - this is an object in array
    InSound *sound = self.playingSoundArray[indexPath.row];//得到当前点击的歌曲的信息
    NSURL *url = [NSURL URLWithString:sound.inSound_Url];
    
    [appDelegate.InPlayerViewController.inplayer playWithUrl:url showView:appDelegate.InPlayerViewController.sliderView];//播放该歌曲的信息
    
    appDelegate.InPlayerViewController.index = indexPath.row;//将歌曲的index传给播放器页面
    
    appDelegate.InPlayerViewController.inSound = sound;
    
//    appDelegate.InPlayerViewController.playingSoundArray = self.playingSoundArray;//播放器正在播放的数组为历史播放记录的数组
    
    [self dismissViewControllerAnimated:YES completion:nil];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
